//
//  AppDelegate.swift
//  WB
//
//  Created by 汪鹏 on 2016/12/14.
//  Copyright © 2016年 汪鹏. All rights reserved.
//

import UIKit
import UserNotifications
import AFNetworking
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // 应用的额外配置
        appAdditionConfig()
        
        window = UIWindow()
        
        window?.backgroundColor = UIColor.red
        
        window?.rootViewController = WPMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadConfigFile()
        
        return true
    }
    
   
    /*
     
    /// 设置整个屏幕的方向
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return .portrait
    }
 
    */

}

// MARK:--应用的额外配置
extension AppDelegate {
    
    func appAdditionConfig() {
        
        // 设置网络指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        // 设置SVProgressHUD移除的最小时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        // ios 10及以上版本
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .carPlay, .sound]) { (success, error) -> () in
                
                print("用户显示通知授权"+(success ? "成功" : "失败"))
            }
            
        } else { // ios 10以下版本
            
            // 取得用户授权显示通知包括：上方的提示条、声音、badgeNumber
            let userNotificationSetting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            
            UIApplication.shared.registerUserNotificationSettings(userNotificationSetting)
        }
        

    }
    
}

// MARK:--模拟从服务器加载配置界面的数据
extension AppDelegate {
    
    
    /// 异步加载数据
    func loadConfigFile() {
        
        DispatchQueue.global().async{
            
            // 获取沙盒路径
            let home_path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            // 拼接文件路径
            let file_path = (home_path as NSString).appendingPathComponent("main2.json")
            
            // 获取bundel文件
            let budle_path = Bundle.main.path(forResource: "main", ofType: "json")
            
            let data = NSData.init(contentsOfFile: budle_path!)
            
            // 将文件写入沙盒
            (data! as NSData).write(toFile: file_path, atomically: true)
        }
        
    }
    
}


